<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
            <el-form-item label="供应商名称" prop="providerId">
                <el-select
                        v-model="queryParams.providerId"
                        placeholder="请选择供应商名称"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in providerOptions"
                            :key="dict.providerId"
                            :label="dict.providerName"
                            :value="dict.providerId"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="申请人" prop="applyUserName">
                <el-input
                        v-model="queryParams.applyUserName"
                        placeholder="请输入申请人"
                        clearable
                        size="small"
                        style="width:180px"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="this.SubPur === 'y'? false :true" @click="handleAuditPass">审核通过</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="this.SubPur === 'y'? false :true" @click="handleAuditNoPass">审核不通过</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="this.SubPur === 'y'? false :true" @click="handleViewItem">查看详情</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table ref="purchaseTableRef" border :data="purchaseTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="单据ID" align="center" width="200" prop="purchaseId" />
            <el-table-column label="供应商" width="180" align="center" prop="providerId" :formatter="providerFormatter" />
            <el-table-column label="采购批发总额" align="center" prop="purchaseTradeTotalAmount">
                <template slot-scope="scope">
                    <span>{{ scope.row.purchaseTradeTotalAmount|rounding }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
            <el-table-column label="申请人" align="center" prop="applyUserName" />
            <el-table-column label="入库操作人" align="center" prop="storageOptUser" />
            <el-table-column label="入库时间" width="180" align="center" prop="storageOptTime" />
            <el-table-column label="审核信息" align="center" prop="examine" />
            <el-table-column label="创建时间" width="180" align="center" prop="createTime" />
        </el-table>
        <!-- 数据表格结束 -->
        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="queryParams.pageNum"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->

        <!-- 单据详情的弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="1000px"
                center
                append-to-body
        >
            <el-table ref="purchaseTableRef" border :data="purchaseItemTableList">
                <el-table-column label="详情ID" width="180" align="center" prop="itemId" />
                <el-table-column label="单据ID" width="200" align="center" prop="purchaseId" />
                <el-table-column label="采购数量" align="center" prop="purchaseNumber" />
                <el-table-column label="批发价" prop="tradePrice" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.tradePrice|rounding }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="批发额" align="center" prop="tradeTotalAmount">
                    <template slot-scope="scope">
                        <span>{{ scope.row.tradeTotalAmount|rounding }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="批次号" align="center" prop="batchNumber" />
                <el-table-column label="药品名称" align="center" prop="medicinesName" />
                <el-table-column label="换算位" align="center" prop="conversion" />
                <el-table-column label="单位" align="center" prop="unit" />
            </el-table>
            <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 单据详情的弹出层结束 -->
        <!-- 弹出来 审核不通过 的表单 -->

        <el-dialog title="提示" :visible.sync="ItemNoPass" width="30%" height="100%"   >
            <el-form ref="ItemNo" :model="ItemNo" >
                <p>请输入不通过的原因</p>
                <el-input type="textarea" v-model="ItemNo.examine"></el-input>

            </el-form>

            <span slot="footer" class="dialog-footer">
				<el-button @click="ItemNoPass = false">取 消</el-button>
				<el-button type="primary" @click="ItemNoPassSub">确 定</el-button>
			</span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "examine",
        // 过滤器
        filters: {
            // 保留两位小数
            rounding(value) {
                return value.toFixed(2)
            }
        },
        data(){
            return{
                //存放 按钮是否显示 数据
                SubPur: "",
                // 是否打开查看详情
                open: false,
                // 弹出层的标题
                title: '',
                // 分页数据总条数
                total: 0,
                currentPage:1,
                pageSize:10,
                // 表格数据
                purchaseTableList: [],
                // 单据详情情数据
                purchaseItemTableList: [],
                // 状态数据字典
                statusOptions: [],
                // 供应商的数据
                providerOptions: [],
                // 查询参数
                queryParams: {
                    providerId: undefined,
                    applyUserName: undefined,
                    status: undefined
                },
                //单据ID
                purchaseTableRef:{},
                // 审核不通过
                ItemNoPass:false,
                // 审核不通过 原因
                ItemNo:{
                    examine:""
                },
            }
        },
        created() {
            //可用供应商
            this.$axios.post("/stock/prov/prov/selectAllProvName/").then(result=>{
                this.providerOptions=result.data.data;
            })
            // 使用全局的根据字典类型查询字典数据的方法查询字典数据
            this.$axios.get("/stock/prod/dict/data/findByType/his_order_status").then(result => {
                this.statusOptions = result.data.data;
            })
            //查询表格数据
            this.initExamine()
        },
        methods:{
            // 查询表格数据
            initExamine() {
                this.$axios.post("/stock/exa/examine/listPurchasePendingForPage/"+this.currentPage+"/"+this.pageSize,this.queryParams).then(res => {
                    this.purchaseTableList = res.data.data.records;
                    this.total = res.data.data.total;
                })
            },
            // 审核不通过 的表单  确认
            ItemNoPassSub(){
                let staus = "4"
                this.$axios.get("/stock/exa/examine/ItemNoPass?status=" + staus + "&" + "purchaseId=" + this.purchaseTableRef[
                    0].purchaseId + "&" + "examine=" + this.ItemNo.examine ).then(res => {
                    if (res.data.data == -1) {
                        this.$message({
                            message: res.data.msg,
                            type: "warning"
                        });
                    } else if (res.data.data == 0) {
                        this.$message({
                            message: res.data.msg,
                            type: "error"
                        });
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: "success"
                        });
                    }
                    this.initExamine()
                })
                this.ItemNoPass=false


            },
            // 条件查询
            handleQuery() {
                this.currentPage=1
                this.initExamine()
            },
            // 重置查询条件
            resetQuery() {
                this.resetForm('queryForm')
                this.dateRange = []
                this.initExamine()
            },
            // 数据表格的多选择框选择时触发
            handleSelectionChnage(row) {
                this.purchaseTableRef = row

                //
                if (row.length == 1) {
                    this.SubPur = 'y'
                } else {
                    this.SubPur = 'n'
                }
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initExamine()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initExamine()
            },
            // 翻译供应商
            providerFormatter(row) {
                let name = ''
                this.providerOptions.filter(item => {
                    if (parseInt(row.providerId) === parseInt(item.providerId)) {
                        name = item.providerName
                    }
                })
                return name
            },
            // 翻译状态
            statusFormatter(row) {
                return this.formatDict(this.statusOptions, row.status)
            },
            // 审核通过
            handleAuditPass() {
                let staus = "3"
                this.public(staus)
                //  主表格  方法  查询
                this.initExamine()
            },

            // 审核不通过
            handleAuditNoPass() {
                //提交 页面状态 审核
                this.ItemNoPass = true;
            },
            public(value) {
                this.$axios.get("/stock/exa/examine/ItemPass?status=" + value + "&" + "purchaseId=" + this.purchaseTableRef[0].purchaseId).then(res => {
                    if (res.data.data == -1) {
                        this.$message({
                            message: res.data.msg,
                            type: "warning"
                        });
                    } else if (res.data.data == 0) {
                        this.$message({
                            message: res.data.msg,
                            type: "error"
                        });
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: "success"
                        });
                    }
                    this.initExamine()
                })
            },
            // 查看详情
            handleViewItem() {
                this.open = true
                let header = "单据编号为【"
                let middle = JSON.stringify(this.purchaseTableRef[0].purchaseId)
                let tail = "】的采购详情数据"
                // 表头
                this.title = header.concat(middle).concat(tail)
                // 显示
                this.providerFormVisible = true;
                // 请求
                this.$axios.post("/stock/exa/examine/purchaseItemAll", this.purchaseTableRef).then(res => {
                    this.purchaseItemTableList = res.data.data.records

                })
            },
            cancel() {
                this.open = false
            }

        }
    }
</script>

<style scoped>

</style>
